﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;

public partial class Month_MonthPlanAddAdmin : System.Web.UI.Page
{
    DepartmentInfoManager dllDepartment = new DepartmentInfoManager();
    IBussinessLogic dllUserInfo = new UserInfoManager();
    MonthPlanManager dllMonthPlanManager = new MonthPlanManager();
    MonthDateManager dllMonthDateManager = new MonthDateManager();
    Authentication dllAuthentication = new Authentication();
    VerifyCircuitManager dllVerifyCircuitManager = new VerifyCircuitManager();
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    Month_RemindofManager dllMonth_RemindofManager = new Month_RemindofManager();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    DataTable _powerTable = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {          
            bindMonthPlan();
            bindDropDownList(this.drpDepartment);
            bindDropDownList(this.drpYear);
            if (Request.Params["Year"] != null && Request.Params["Year"].ToString().Length != 0)
            {
                string Year = Server.UrlDecode(Request.Params["Year"].ToString());
                this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(Year));
            }
            bindDropDownList(this.drpQuarterDate);
            if (Request.Params["Quarter"] != null && Request.Params["Quarter"].ToString().Length != 0)
            {
                string Quarter = Server.UrlDecode(Request.Params["Quarter"].ToString());
                this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByValue(Quarter));
            }
            bindDropDownList(this.drpMonthDate);
            if (Request.Params["Month"] != null && Request.Params["Month"].ToString().Length != 0)
            {
                string Month = Server.UrlDecode(Request.Params["Month"].ToString());
                this.drpMonthDate.SelectedIndex = this.drpMonthDate.Items.IndexOf(this.drpMonthDate.Items.FindByValue(Month));
            }
            BindMonthRemind();            
        }
    }


    /// <summary>
    /// 绑定月提醒
    /// </summary>
    /// <author>gzz</author>
    /// <date>2009-1-4</date>
    void BindMonthRemind()
    {
        //获得Session中登陆用户的用户名
        object[] _user = new object[1];
        _user[0] = objUserInfo.UserName;
        //根据用户名获得用户真实姓名
        DataTable _userInfo = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _user);
        object[] _value = new object[2];
        _value[0] = this.drpDepartment.SelectedItem.Text;
        _value[1] = Convert.ToInt32(this.drpMonthDate.SelectedItem.Value);
        //根据用户所在部门和当前时间查询月工作提醒
        DataTable _monthRemind = dllMonth_RemindofManager.get(Month_RemindofManager.field.DepartmentName.ToString(), Month_RemindofManager.field.MonthDate.ToString(), _value);
        if (_monthRemind.Rows.Count > 0)
        {
            this.gdvMonthRemind.DataSource = _monthRemind;
            this.gdvMonthRemind.DataBind();
        }
        else
        {
            this.gdvMonthRemind.DataSource = new DataTable();
            this.gdvMonthRemind.DataBind();
        }
    }


    /// <summary>
    /// 创建空DataTable用于绑定GridView
    /// </summary>
    /// <returns></returns>
    DataTable createDataTable()
    {
        DataTable _dataTable = new DataTable();
        _dataTable.Columns.Add("MonthPlanId");
        _dataTable.Columns.Add("MonthPlanSortId");
        _dataTable.Columns.Add("Source");
        _dataTable.Columns.Add("CompleteTime");
        _dataTable.Columns.Add("WorkItem");
        _dataTable.Columns.Add("MasterDepartment");
        _dataTable.Columns.Add("MasterPeople");
        _dataTable.Columns.Add("SlaveDepartment");
        _dataTable.Columns.Add("SlaveDepartmentDuty");
        _dataTable.Columns.Add("EvaluatePeople");
        _dataTable.Columns.Add("Remart");
        _dataTable.Columns.Add("PlanStep");
        return _dataTable;
    }



    /// <summary>
    /// 绑定空记录表头gridview
    /// </summary>
    void bindMonthPlan()
    {
        DataTable _monthPlan = createDataTable();
        DataRow dr = _monthPlan.NewRow();
        _monthPlan.Rows.Add(dr);
        this.gdvMonthPlan.DataSource = _monthPlan;
        this.gdvMonthPlan.DataBind();
        for (int i = 0; i < this.gdvMonthPlan.Rows.Count; i++)
        {
            //LinkButton lbtn = (LinkButton)gdvMonthPlan.Rows[i].Cells[5].FindControl("lbtnSource");
            //lbtn.Text = gdvMonthPlan.DataKeys[i]["Source"].ToString();
            wrapByComma(this.gdvMonthPlan, i, 6);
            wrapByComma(this.gdvMonthPlan, i, 7);
            this.gdvMonthPlan.Rows[i].Cells[10].Text = this.gdvMonthPlan.Rows[i].Cells[10].Text.Replace("&", "");
            this.gdvMonthPlan.Rows[i].Cells[10].Text = this.gdvMonthPlan.Rows[i].Cells[10].Text.Replace("nbsp;", "");
            this.gdvMonthPlan.Rows[i].Cells[10].Text = this.gdvMonthPlan.Rows[i].Cells[10].Text.Replace("amp;", "");
        }
        this.gdvMonthPlan.Rows[0].Visible = false;
    }


    /// <summary>
    /// 绑定所有的dropdownlist
    /// </summary>
    /// <param name="_drp"></param>
    void bindDropDownList(DropDownList _drp)
    {
        //部门绑定
        if (_drp.Equals(this.drpDepartment))
        {
            _drp.DataSource = dllDepartment.getNoTree();
            _drp.DataTextField = "DepartmentName";
            _drp.DataValueField = "DepartmentId";
            _drp.DataBind();
        }
        //年份绑定
        if (_drp.Equals(this.drpYear))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem _li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(_li);
            }
            //根据系统日期自动绑定到当前年份
            //string _year = DateTime.Now.Year.ToString();
            //this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year));
            //根据系统当前日期查找到对应的年周期
            DataTable _year = dllQuarterDateManager.getQuarterDate();
            this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year.Rows[0][2].ToString()));

        }

        //绑定年计划周期
        if (_drp.Equals(this.drpQuarterDate))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYear.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                this.drpQuarterDate.DataSource = _quarter;
                this.drpQuarterDate.DataTextField = "QuarterName";
                this.drpQuarterDate.DataValueField = "Quarter_Id";
                this.drpQuarterDate.DataBind();
                #region dropdownlist显示系统所对应的当前年周期
                //根据系统当前日期查找到对应的年周期
                DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                //如果查到结果（结果只有一条）

                if (_quartertime.Rows.Count == 1)
                {
                    this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                    this.drpQuarterDate.DataSource = _quartertime;
                    this.drpQuarterDate.DataTextField = "QuarterName";
                    this.drpQuarterDate.DataValueField = "Quarter_Id";
                }
                #endregion
                bindDropDownList(this.drpMonthDate);
            }
            else
            {
                ListItem _li = new ListItem("无年周期", "0");
                this.drpQuarterDate.Items.Clear();
                this.drpQuarterDate.Items.Add(_li);
                bindDropDownList(this.drpMonthDate);
            }

        }

        //绑定月计划周期
        if (_drp.Equals(this.drpMonthDate))
        {

            object[] _value = new object[2];
            _value[0] = this.drpYear.SelectedValue;
            _value[1] = this.drpQuarterDate.SelectedItem.Text;
            DataTable _month = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value);
            if (_month.Rows.Count != 0)
            {
                this.drpMonthDate.DataSource = _month;
                this.drpMonthDate.DataTextField = "MonthName";
                this.drpMonthDate.DataValueField = "Month_Id";
                this.drpMonthDate.DataBind();
                #region 根据系统当前日期绑定到当前月周期
                DataTable _monthtime = dllMonthDateManager.getMonthDate();
                if (_monthtime.Rows.Count == 1)
                {
                    this.drpMonthDate.SelectedIndex = this.drpMonthDate.Items.IndexOf(this.drpMonthDate.Items.FindByText(_monthtime.Rows[0][3].ToString()));
                    this.drpMonthDate.DataSource = _monthtime;
                    this.drpMonthDate.DataTextField = "MonthName";
                    this.drpMonthDate.DataValueField = "Month_Id";

                }
                #endregion

            }
            else
            {
                ListItem _li = new ListItem("无月周期", "0");
                this.drpMonthDate.Items.Clear();
                this.drpMonthDate.Items.Add(_li);
            }
        }
    }


    int isNull()
    {
        #region 验证是否为空
        if (this.txtMonthPlanSortId.Value.Length == 0)
        {
            return 0;
        }
        if (this.txtCompleteTime.Text.Length == 0)
        {
            return 0;
        }
        if (this.txtMasterPeople.Value.Length == 0)
        {
            return 0;
        }
        if (this.txtWorkItem.Text.Length == 0)
        {
            return 0;
        }
        if (this.txtSlaveDepartment.Value.Length == 0)
        {
            return 0;
        }
        //if (this.txtSlaveDepartment.Value != "独立完成")
        //{
        //    //if (this.txtSlaveDepartmentDuty.Text.Length < 18)
        //    //{
        //        return 0;
        //    //}
        //}
        if (this.txtEvaluatePeople.Value.Length == 0)
        {
            return 0;
        }
        return 1;
        #endregion
    }

    /// <summary>
    /// 时间验证
    /// </summary>
    bool verifyDate()
    {
        #region 必要验证判断
        //判断完成时间是否大于起始时间
        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(this.drpMonthDate.SelectedItem.Value);
        DataTable _monthDate = dllMonthDateManager.get(MonthDateManager.field.Month_Id.ToString(), _value);
        if (_monthDate.Rows.Count > 0)
        {
            DateTime _startTime = Convert.ToDateTime(_monthDate.Rows[0]["MonthStartDay"].ToString());
            DateTime _endTime = Convert.ToDateTime(this.txtCompleteTime.Text.Trim());
            if (!dllAuthentication.IsEndTimeGreaterThanStartTime(_startTime, _endTime))
            {
                Utility.HttpHelp.alert("完成时间需要大于本月起始时间", Page);
                return false;
            }
        }
        return true;
        #endregion
    }

    /// <summary>
    /// 添加月计划方法
    /// </summary>
    /// <param name="state">0草稿 1 提交</param>    
    void addMonthPlan(int state)
    {

        #region 参数
        int _count = 0;
        List<object[]> _valueList = new List<object[]>();
        //得到参数 部门名称的DataTable
        object[] _valueDepartmentName = new object[1];
        _valueDepartmentName[0] = this.drpDepartment.SelectedItem.Text;
        DataTable _VerifyCircuitId = dllVerifyCircuitManager.getVerifyCircuitIdByDepartment(_valueDepartmentName);
        if (_VerifyCircuitId.Rows.Count == 0)
        {
            Utility.Error.RedirectErrorPage("所在部门无计划流程，请重试");
        }


        //获取登录用户名
        object[] _user = new object[1];
        _user[0] = this.txtSponsor.Value;
        DataTable _usertable = dllUserInfo.get(UserInfoManager.field.RealName.ToString(), _user);
        if (_usertable.Rows.Count == 0)
        {
            return;
        }
        else
        {
            for (int i = 1; i < this.gdvMonthPlan.Rows.Count; i++)
            {
                CheckBox _ckb = (CheckBox)this.gdvMonthPlan.Rows[i].Cells[0].FindControl("ckbSelect");
                if (_ckb.Checked == true)
                {
                    _count++;
                    object[] _value = new object[17];     
                    //完成时间
                    _value[0] = Convert.ToDateTime(this.gdvMonthPlan.Rows[i].Cells[2].Text.Trim());
                    //工作内容
                    _value[1] = this.gdvMonthPlan.Rows[i].Cells[3].Text.Trim();
                    _value[2] = this.gdvMonthPlan.Rows[i].Cells[4].Text.Trim();
                    //责任部门
                    _value[3] = this.drpDepartment.SelectedItem.Text;
                    //责任人
                    _value[4] = this.gdvMonthPlan.Rows[i].Cells[6].Text.Trim().Replace("<br>", ",");
                    //配合部门
                    _value[5] = this.gdvMonthPlan.Rows[i].Cells[7].Text.Trim().Replace("<br>", ",");
                    //配合部门责任
                    _value[6] = this.gdvMonthPlan.Rows[i].Cells[8].Text.Trim();
                    _value[7] = state;//发送状态
                    _value[8] = 0;//是否达成共识
                    //备注
                    _value[9] = this.gdvMonthPlan.Rows[i].Cells[10].Text.Trim(); ;//Remart
                    _value[10] = Convert.ToInt32(this.drpMonthDate.SelectedValue);//MonthDate
                    _value[11] = Convert.ToInt32(_VerifyCircuitId.Rows[0]["VerifyCircuitId"].ToString());
                    _value[12] = _usertable.Rows[0]["UserName"].ToString();
                    _value[13] = this.gdvMonthPlan.Rows[i].Cells[9].Text.Trim();//评估人
                    _value[14] = this.drpSource.SelectedItem.Text;
                    _value[15] = 0;
                    _value[16] = Convert.ToInt32(this.gdvMonthPlan.Rows[i].Cells[1].Text.Trim());
                    _valueList.Add(_value);
                }

            }
        #endregion
            if (_count <= 0)
            {
                Utility.HttpHelp.alert("请选择要保存的计划", Page);
                return;
            }
            if (_valueList.Count > 0)
            {
                int result = dllMonthPlanManager.addWithTransaction(_valueList);
                if (result == 0)
                {
                    Utility.Error.RedirectErrorPage("添加年计划未成功，请重试！");
                }

                #region 善后操作
                //将文本框清空
                this.txtMonthPlanSortId.Value = this.gdvMonthPlan.Rows.Count.ToString();
                this.txtCompleteTime.Text = "";
                this.txtMasterPeople.Value = "";
                this.txtRemark.Text = "";
                this.txtSlaveDepartment.Value = "";
                this.txtSlaveDepartmentDuty.Text = "";
                this.txtEvaluatePeople.Value = "";
                this.txtWorkItem.Text = "";
                #endregion
            }

        }
    }

    /// <summary>
    /// 保存按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //排除之前设定的空行
        if (this.gdvMonthPlan.Rows.Count > 1)
        {
            if (this.gdvMonthPlan.DataKeys[1]["MonthPlanId"].ToString().Length == 0 || this.gdvMonthPlan.DataKeys[1]["MonthPlanId"].ToString() == "&nbsp;")
            {
                addMonthPlan(0);
                //读取该月份该部门下所有月计划（带有id）并绑定
                object[] _value = new object[1];
                _value[0] = Convert.ToInt32(this.drpMonthDate.SelectedValue);
                DataTable _monthPlan = dllMonthPlanManager.get(MonthPlanManager.field.PlanSendState.ToString(), _value);
                DataRow dr = _monthPlan.NewRow();
                _monthPlan.Rows.InsertAt(dr, 0);
                DataView dv = new DataView(_monthPlan);
                dv.Sort = "MonthPlanSortId";
                this.gdvMonthPlan.DataSource = dv;
                this.gdvMonthPlan.DataBind();
                for (int i = 0; i < this.gdvMonthPlan.Rows.Count; i++)
                {
                    wrapByComma(this.gdvMonthPlan, i, 6);
                    wrapByComma(this.gdvMonthPlan, i, 7);
                    string _Lengthstart = this.gdvMonthPlan.Rows[i].Cells[2].Text;
                    string _Lengthend = this.gdvMonthPlan.Rows[i].Cells[3].Text;
                    this.gdvMonthPlan.Rows[i].Cells[10].Text = this.gdvMonthPlan.Rows[i].Cells[10].Text.Replace("&", "");
                    this.gdvMonthPlan.Rows[i].Cells[10].Text = this.gdvMonthPlan.Rows[i].Cells[10].Text.Replace("nbsp;", "");
                    this.gdvMonthPlan.Rows[i].Cells[10].Text = this.gdvMonthPlan.Rows[i].Cells[10].Text.Replace("amp;", "");
                    if (_Lengthstart.Length > 9)
                    {
                        _Lengthstart = _Lengthstart.Substring(0, 9);//截取字符串，从左边截取10个字符串
                        this.gdvMonthPlan.Rows[i].Cells[2].Text = _Lengthstart;
                    }
                    if (_Lengthend.Length > 9)
                    {
                        _Lengthend = _Lengthend.Substring(0, 9);//截取字符串，从左边截取10个字符串
                        this.gdvMonthPlan.Rows[i].Cells[3].Text = _Lengthend;
                    }

                }
                this.gdvMonthPlan.Rows[0].Visible = false;
            }
            else
            {
                Utility.HttpHelp.alert("计划已保存！", Page);
            }

        }
        else
        {
            Utility.HttpHelp.alert("您未添加月计划", Page);
        }
    }



    /// <summary>
    /// 修改月计划方法
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-3-7</date>
    void editMonthPlan()
    {
        #region 参数
        int _count = 0;
        List<object[]> _valueList = new List<object[]>();
        for (int i = 1; i < this.gdvMonthPlan.Rows.Count; i++)
        {
            CheckBox _ckb = (CheckBox)this.gdvMonthPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (_ckb.Checked == true)
            {
                _count++;
                object[] _value = new object[2];
                _value[0] = Convert.ToInt32(this.gdvMonthPlan.DataKeys[i]["MonthPlanId"].ToString());
                _valueList.Add(_value);
            }
        }
        #endregion
        if (_count <= 0)
        {
            Utility.HttpHelp.alert("请选择要提交的计划", Page);
            return;
        }
        if (_valueList.Count > 0)
        {
            int result = dllMonthPlanManager.editWithTransaction(MonthPlanManager.field.admin.ToString(), _valueList);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("提交月计划未成功，请重试！");
            }
            else
            {
                DataTable _month = createDataTable();
                DataRow _dr = _month.NewRow();
                _month.Rows.Add(_dr);
                this.gdvMonthPlan.DataSource = _month;
                this.gdvMonthPlan.DataBind();
                this.gdvMonthPlan.Rows[0].Visible = false;

            }

            #region 善后操作
            //将文本框清空
            this.txtMonthPlanSortId.Value = this.gdvMonthPlan.Rows.Count.ToString();
            this.txtCompleteTime.Text = "";
            this.txtMasterPeople.Value = "";
            this.txtRemark.Text = "";
            this.txtSlaveDepartment.Value = "";
            this.txtSlaveDepartmentDuty.Text = "";
            this.txtEvaluatePeople.Value = "";
            this.txtWorkItem.Text = "";
            #endregion
        }
    }


    /// <summary>
    /// 提交按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOk_Click(object sender, EventArgs e)
    {
        //排除之前设定的空行
        if (this.gdvMonthPlan.Rows.Count > 1)
        {
            if (this.gdvMonthPlan.DataKeys[1]["MonthPlanId"].ToString().Length == 0 || this.gdvMonthPlan.DataKeys[1]["MonthPlanId"].ToString() == "&nbsp;")
            {
                addMonthPlan(3);         
                bindMonthPlan();
            }
            else
            {
                editMonthPlan();
            }
        }
        else
        {
            Utility.HttpHelp.alert("您未添加月计划", Page);
        }
        Utility.HttpHelp.closeWindow("MonthPlanDisplay.aspx?Id=" + this.drpMonthDate.SelectedValue + "&depart=" + this.drpDepartment.SelectedValue, Page);
    }


    /// <summary>
    /// 全选事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ckbAllSelect_CheckedChanged(object sender, EventArgs e)
    {
        if (this.ckbAllSelect.Checked == true)
        {
            for (int i = 0; i < this.gdvMonthPlan.Rows.Count; i++)
            {
                CheckBox _chk = ((CheckBox)this.gdvMonthPlan.Rows[i].Cells[0].FindControl("ckbSelect"));
                _chk.Checked = true;
            }
        }
        else
        {
            for (int i = 0; i < this.gdvMonthPlan.Rows.Count; i++)
            {
                CheckBox _chk = ((CheckBox)this.gdvMonthPlan.Rows[i].Cells[0].FindControl("ckbSelect"));
                _chk.Checked = false;
            }
        }
    }
    /// <summary>
    /// 增加一行事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        int _isnull = 1;// isNull();
        if (_isnull == 0)
        {
            Utility.HttpHelp.alert("请输入完整", Page);
        }
        else
        {
            if (this.txtSponsor.Value.Length == 0)
            {
                Utility.HttpHelp.alert("请选择发起人", Page);
                return;
            }
            if (this.txtSponsor.Value.Contains(","))
            {
                Utility.HttpHelp.alert("发起人只能选择一位", Page);
                return;
            }

            if (!verifyDate())
            {
                return;
            }
            if (this.txtSlaveDepartment.Value != "独立完成")
            {
                if (this.txtSlaveDepartmentDuty.Text.Length < 18)
                {
                    Utility.HttpHelp.alert("配合部门责任字数不得低于32个字符", Page);
                    return;
                }
            }
            if (this.txtPlanStep.Text.Length == 0)
            {
                Utility.HttpHelp.alert("实施步骤不可以为空", Page);
                return;
            }
            
            ListItem _li = this.drpMonthDate.SelectedItem;
            if (_li.Text == "无月周期")
            {
                Utility.HttpHelp.alert("请选择月周期", Page);
                return;
            }
            DataTable _monthPlan = createDataTable();
            //获取当前gridview的内容导入到datatable
            for (int i = 0; i < this.gdvMonthPlan.Rows.Count; i++)
            {
                DataRow _drGridView = _monthPlan.NewRow();
                _drGridView["MonthPlanId"] = "";
                _drGridView["MonthPlanSortId"] = this.gdvMonthPlan.Rows[i].Cells[1].Text.ToString();
                _drGridView["Source"] = this.gdvMonthPlan.Rows[i].Cells[5].Text.ToString();
                _drGridView["CompleteTime"] = this.gdvMonthPlan.Rows[i].Cells[2].Text.ToString();
                _drGridView["WorkItem"] = this.gdvMonthPlan.Rows[i].Cells[3].Text.ToString();
                _drGridView["MasterDepartment"] = this.drpDepartment.SelectedItem.Text;
                _drGridView["MasterPeople"] = this.gdvMonthPlan.Rows[i].Cells[5].Text.ToString();
                _drGridView["SlaveDepartment"] = this.gdvMonthPlan.Rows[i].Cells[7].Text.ToString();
                _drGridView["SlaveDepartmentDuty"] = this.gdvMonthPlan.Rows[i].Cells[8].Text.ToString();
                _drGridView["EvaluatePeople"] = this.gdvMonthPlan.Rows[i].Cells[9].Text.ToString();
                _drGridView["Remart"] = this.gdvMonthPlan.Rows[i].Cells[10].Text.ToString();
                _drGridView["PlanStep"] = this.gdvMonthPlan.Rows[i].Cells[4].Text.ToString();
                _monthPlan.Rows.Add(_drGridView);
                #region 去除特殊符号如&nbsp;
                _drGridView["MonthPlanSortId"] = _drGridView["MonthPlanSortId"].ToString().Replace("&", "");
                _drGridView["MonthPlanSortId"] = _drGridView["MonthPlanSortId"].ToString().Replace("nbsp;", "");
                _drGridView["Source"] = _drGridView["Source"].ToString().Replace("&", "");
                _drGridView["Source"] = _drGridView["Source"].ToString().Replace("nbsp;", "");
                _drGridView["CompleteTime"] = _drGridView["CompleteTime"].ToString().Replace("&", "");
                _drGridView["CompleteTime"] = _drGridView["CompleteTime"].ToString().Replace("nbsp;", "");
                _drGridView["WorkItem"] = _drGridView["WorkItem"].ToString().Replace("&", "");
                _drGridView["WorkItem"] = _drGridView["WorkItem"].ToString().Replace("nbsp;", "");

                _drGridView["MasterDepartment"] = _drGridView["MasterDepartment"].ToString().Replace("&", "");
                _drGridView["MasterDepartment"] = _drGridView["MasterDepartment"].ToString().Replace("nbsp;", "");
                _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace("&", "");
                _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace("nbsp;", "");
                _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace("&", "");
                _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace("nbsp;", "");
                _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace(",<br>", ",");

                _drGridView["SlaveDepartmentDuty"] = _drGridView["SlaveDepartmentDuty"].ToString().Replace("&", "");
                _drGridView["SlaveDepartmentDuty"] = _drGridView["SlaveDepartmentDuty"].ToString().Replace("nbsp;", "");
                _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace("&", "");
                _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace("nbsp;", "");
                _drGridView["Remart"] = _drGridView["Remart"].ToString().Replace("&", "");
                _drGridView["Remart"] = _drGridView["Remart"].ToString().Replace("nbsp;", "");
                _drGridView["PlanStep"] = _drGridView["PlanStep"].ToString().Replace("&", "");
                _drGridView["PlanStep"] = _drGridView["PlanStep"].ToString().Replace("nbsp;", "");
                #endregion
            }
            //获取当前添加的信息内容
            DataRow _drTable = _monthPlan.NewRow();
            _drTable["MonthPlanSortId"] = this.txtMonthPlanSortId.Value.Trim();
            _drTable["Source"] = this.drpSource.SelectedItem.Text;
            _drTable["CompleteTime"] = this.txtCompleteTime.Text.Trim();
            _drTable["WorkItem"] = this.txtWorkItem.Text.Trim();
            _drTable["MasterDepartment"] = this.drpDepartment.SelectedItem.Text;
            _drTable["MasterPeople"] = this.txtMasterPeople.Value.Trim();
            _drTable["SlaveDepartment"] = this.txtSlaveDepartment.Value.Trim();
            _drTable["SlaveDepartmentDuty"] = this.txtSlaveDepartmentDuty.Text.Trim();
            _drTable["EvaluatePeople"] = this.txtEvaluatePeople.Value.Trim();
            _drTable["Remart"] = this.txtRemark.Text;
            _drTable["PlanStep"] = this.txtPlanStep.Text.Trim();
            _monthPlan.Rows.Add(_drTable);
            DataView dv = new DataView(_monthPlan);
            dv.Sort = "MonthPlanSortId";
            this.gdvMonthPlan.DataSource = dv;
            this.gdvMonthPlan.DataBind();
            //隐藏首行，只保留表头
            this.gdvMonthPlan.Rows[0].Visible = false;
            for (int i = 0; i < this.gdvMonthPlan.Rows.Count; i++)
            {
                wrapByComma(this.gdvMonthPlan, i, 6);
                wrapByComma(this.gdvMonthPlan, i, 7);
            }
            #region 善后操作
            //将文本框清空
            this.txtMonthPlanSortId.Value = this.gdvMonthPlan.Rows.Count.ToString();
            this.txtCompleteTime.Text = "";
            this.txtMasterPeople.Value = "";
            this.txtRemark.Text = "";
            this.txtSlaveDepartment.Value = "";
            this.txtSlaveDepartmentDuty.Text = "";
            this.txtEvaluatePeople.Value = "";
            this.txtWorkItem.Text = "";
            this.txtPlanStep.Text = "";
            #endregion
        }
    }
    /// <summary>
    /// 根据逗号换行事件
    /// </summary>
    void wrapByComma(GridView _gridView, int _rowIndex, int _cells)
    {
        string _content = _gridView.Rows[_rowIndex].Cells[_cells].Text;
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&lt;br&gt;", "<br>");
        //_gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&gt;", ">");
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _gridView.Rows[_rowIndex].Cells[_cells].Text.Replace(",", "<br>");


    }
    /// <summary>
    /// 删除按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        //得到button所在行        
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        int _rowIndex = iRow.RowIndex;//得到该行的索引
        //遍历GridView将GridView转为DataTable
        DataTable _monthPlan = createDataTable();
        //获取GridView当前内容导入DataTable

        for (int i = 0; i < this.gdvMonthPlan.Rows.Count; i++)
        {
            DataRow _drGridView = _monthPlan.NewRow();
            _drGridView["MonthPlanId"] = this.gdvMonthPlan.DataKeys[i]["MonthPlanId"].ToString();
            _drGridView["MonthPlanSortId"] = this.gdvMonthPlan.Rows[i].Cells[1].Text;
            
            _drGridView["CompleteTime"] = this.gdvMonthPlan.Rows[i].Cells[3].Text.ToString();
            _drGridView["WorkItem"] = this.gdvMonthPlan.Rows[i].Cells[4].Text.ToString();
            _drGridView["MasterDepartment"] = this.gdvMonthPlan.Rows[i].Cells[5].Text.ToString();
            _drGridView["MasterPeople"] = this.gdvMonthPlan.Rows[i].Cells[6].Text.ToString();
            _drGridView["SlaveDepartment"] = this.gdvMonthPlan.Rows[i].Cells[7].Text.ToString();
            _drGridView["SlaveDepartmentDuty"] = this.gdvMonthPlan.Rows[i].Cells[8].Text.ToString();
            _drGridView["EvaluatePeople"] = this.gdvMonthPlan.Rows[i].Cells[9].Text.ToString();
            _drGridView["Remart"] = this.gdvMonthPlan.Rows[i].Cells[10].Text.ToString();
            #region 去除特殊符号如&nbsp;
            _drGridView["MonthPlanId"] = _drGridView["MonthPlanId"].ToString().Replace("&", "");
            _drGridView["MonthPlanId"] = _drGridView["MonthPlanId"].ToString().Replace("nbsp;", "");
            _drGridView["MonthPlanSortId"] = _drGridView["MonthPlanSortId"].ToString().Replace("&", "");
            _drGridView["MonthPlanSortId"] = _drGridView["MonthPlanSortId"].ToString().Replace("nbsp;", "");
            
            _drGridView["CompleteTime"] = _drGridView["CompleteTime"].ToString().Replace("&", "");
            _drGridView["CompleteTime"] = _drGridView["CompleteTime"].ToString().Replace("nbsp;", "");
            _drGridView["WorkItem"] = _drGridView["WorkItem"].ToString().Replace("&", "");
            _drGridView["WorkItem"] = _drGridView["WorkItem"].ToString().Replace("nbsp;", "");

            _drGridView["MasterDepartment"] = _drGridView["MasterDepartment"].ToString().Replace("&", "");
            _drGridView["MasterDepartment"] = _drGridView["MasterDepartment"].ToString().Replace("nbsp;", "");
            _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace("&", "");
            _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace("nbsp;", "");
            _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace("&", "");
            _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace("nbsp;", "");
            _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace(",<br>", ",");

            _drGridView["SlaveDepartmentDuty"] = _drGridView["SlaveDepartmentDuty"].ToString().Replace("&", "");
            _drGridView["SlaveDepartmentDuty"] = _drGridView["SlaveDepartmentDuty"].ToString().Replace("nbsp;", "");
            _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace("&", "");
            _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace("nbsp;", "");
            _drGridView["Remart"] = _drGridView["Remart"].ToString().Replace("&", "");
            _drGridView["Remart"] = _drGridView["Remart"].ToString().Replace("nbsp;", "");
            #endregion
            _monthPlan.Rows.Add(_drGridView);
        }
        _monthPlan.Rows[_rowIndex].Delete();
        //计划如果被保存为草稿，也同时删除
        if (this.gdvMonthPlan.DataKeys[_rowIndex]["MonthPlanId"].ToString() != "&nbsp;" && this.gdvMonthPlan.DataKeys[_rowIndex]["MonthPlanId"].ToString().Length > 0)
        {

            object[] _value = new object[1];
            _value[0] = Convert.ToInt32(this.gdvMonthPlan.DataKeys[_rowIndex]["MonthPlanId"].ToString());
            int result = dllMonthPlanManager.del(MonthPlanManager.field.MonthPlanId.ToString(), _value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("删除月计划未成功，请重试！");
            }
        }
        this.gdvMonthPlan.DataSource = _monthPlan;
        this.gdvMonthPlan.DataBind();
        //隐藏首行，只保留表头
        this.gdvMonthPlan.Rows[0].Visible = false;
        for (int i = 0; i < this.gdvMonthPlan.Rows.Count; i++)
        {
            wrapByComma(this.gdvMonthPlan, i, 6);
            wrapByComma(this.gdvMonthPlan, i, 7);
        }
    }
    #region DropDownList索引改变事件
    /// <summary>
    /// DropDownList索引改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpQuarterDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpMonthDate);
    }
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDate);
        bindDropDownList(this.drpMonthDate);

    }
    protected void drpMonthDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindMonthRemind();
    }
    #endregion


    /// <summary>
    /// 添加月工作提醒
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btntijiao_Click(object sender, EventArgs e)
    {
        Model.UserInfo objUserInfo = new Model.UserInfo();
        object[] _user = new object[1];
        _user[0] = objUserInfo.UserName;
        DataTable _userTable = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _user);
        if (_userTable.Rows.Count > 0)
        {
            object[] _value = new object[4];
            _value[0] = _userTable.Rows[0]["RealName"].ToString();
            _value[1] = this.drpDepartment.SelectedItem.Text;
            _value[2] = Convert.ToInt32(this.drpMonthDate.SelectedItem.Value);
            _value[3] = this.txtTheMonthRemind.Text;
            int result = dllMonth_RemindofManager.add(_value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("添加月工作提醒未成功，请重试！");
            }
            else
            {
                BindMonthRemind();
            }
        }
        else
        {
            dllAuthentication.isLogin();
        }
        this.txtTheMonthRemind.Text = "";

    }


    /// <summary>
    /// 删除月提醒事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-3-6</date>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(this.gdvMonthRemind.Rows[i].Cells[0].Text);
        int result = dllMonth_RemindofManager.del(Month_RemindofManager.field.RemindId.ToString(), _value);
        if (result == 0)
        {
            Utility.Error.RedirectErrorPage("删除月工作提醒未成功，请重试！");
        }
        else
        {
            BindMonthRemind();
        }
    }
}
